﻿using System;
using System.Collections.Generic;
using System.Text;
using WindowService;
using Echevil;

namespace ConsoleHardwareMonitor
{
    public enum NetType
    {
        NET = 0,
        WIFI = 2,
        NET3G = 4
    }

    public enum LinkState
    {
        OK = 1,
        NO = 0
    }
    public class NetHelper
    {
        private G3Helper g3Helper = null;
        public NetHelper(string portName, int baudRate = 9600, int timeOut = 30000, string networkCardName = "", string apnName = "3GNET")
        {
            if (!string.IsNullOrEmpty(portName))
            {
                this.g3Helper = new G3Helper(portName, baudRate, timeOut);
                this.g3Helper.NetworkCardName = networkCardName;
                this.g3Helper.ApnName = apnName;
            }
            else
            {
                Log.WriteLog("NetHelper 3G Module is not exist", LogType.Info);
            }
        }

        public NetType getNetType()
        {
            if (g3Helper == null)
            {
                Log.WriteLog("NetHelper Network Type is:" + NetType.NET, LogType.Info);
                return NetType.NET;
            }
            Log.WriteLog("NetHelper Network Type is:" + NetType.NET3G, LogType.Trace);
            return NetType.NET3G;
        }

        public LinkState getLinkState()
        {
            if (g3Helper == null)
            {
                Log.WriteLog("NetHelper Link State is No, because 3G Module is not exist", LogType.Info);
                return LinkState.NO;
            }
            LinkState state = g3Helper.isAttached() ? LinkState.OK : LinkState.NO;

            Log.WriteLog("NetHelper Link State is " + state.ToString(), LogType.Trace);
            return state;
        }

        public string getNetSpeed()
        {
            if (g3Helper == null)
            {
                Log.WriteLog("NetHelper Network Speed is 0 kbps, because 3G Module is not exist", LogType.Info);
                return "0 kbps";
            }
            string wSpeed = g3Helper.getWSpeed();

            Log.WriteLog("NetHelper Network Speed is " + wSpeed.ToString(), LogType.Trace);
            return wSpeed;
        }
        public string getWsig()
        {
            if (g3Helper == null)
            {
                Log.WriteLog("NetHelper Wsig is NULL, because 3G Module is not exist", LogType.Info);
                return "";
            }
            string wsig = g3Helper.getWsig();

            Log.WriteLog("NetHelper Wsig is " + wsig.ToString(), LogType.Trace);
            return wsig;
        }
        public void call()
        {
            if (g3Helper == null)
            {
                Log.WriteLog("NetHelper CallNet is not to calling, because 3G Module is not exist", LogType.Info);
                return;
            }
            g3Helper.connect();
            Log.WriteLog("NetHelper CallNet is calling", LogType.Trace);
        }

    }
}
